package com.jz.kufangkuqu.kuqu.service.impl;

import com.jz.beijianbao.beijian.personEnum.PersonEnum;
import com.jz.kufangkuqu.kuqu.bean.*;
import com.jz.kufangkuqu.kuqu.mapper.KuQuMapper;
import com.jz.kufangkuqu.kuqu.service.KuQuService;
import com.jz.util.AllRecordsVo;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class KuQuServiceImpl implements KuQuService {

    @Autowired
    private KuQuMapper kuQuMapper;



    @Override
    public ResultVO queryPage(KuQuSelect kuQuSelectVo) {
        List<KuQu> kuQuList = kuQuMapper.queryPage(kuQuSelectVo);
        for (KuQu plant: kuQuList) {
            Integer enabled = plant.getStatus() ? 1:0;
            plant.setStatusName(PersonEnum.getName(enabled));
        }
        int count = kuQuMapper.count(kuQuSelectVo);
        AllRecordsVo allRecordsVo = new AllRecordsVo();
        allRecordsVo.setPageIndex(kuQuSelectVo.getPageIndex());
        allRecordsVo.setPageSize(kuQuSelectVo.getPageSize());
        allRecordsVo.setTotalSize(count);
        allRecordsVo.countPage(count);
        allRecordsVo.setDataList(kuQuList);
        return ResultBuildVO.success(allRecordsVo);
    }


    @Override
    public ResultVO add(KuQuAdd kuQuAddList) {
        Date now = new Date();
        KuQu legalPlantVo1 = new KuQu();
        BeanUtils.copyProperties(kuQuAddList,legalPlantVo1);
        legalPlantVo1.setCreateName("从session中获取");
        legalPlantVo1.setCreateTime(now);
        kuQuMapper.add(legalPlantVo1);
        kuQuMapper.addShuXing(legalPlantVo1);
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO updateHuiXian(KuQuUpdateHuiXian kuQuUpdate) {
        KuQuFangHui kuQuFangHui = kuQuMapper.updateHuiXian(kuQuUpdate);
        List<ShuXingXiaLa> shuXingXiaLas = kuQuMapper.queryShuxinng(kuQuFangHui.getCode());
        kuQuFangHui.setBusinessAttribute(shuXingXiaLas);
        return ResultBuildVO.success(kuQuFangHui);
    }


    @Override
    public ResultVO update(KuQuUpdate kuQuUpdate) {
        Date now = new Date();
        KuQu legalPlant1 = new KuQu();
        BeanUtils.copyProperties(kuQuUpdate,legalPlant1);
        legalPlant1.setUpdateName("从session获取");
        legalPlant1.setUpdateTime(now);
        int version = kuQuMapper.updatePlant(legalPlant1);
        if (version==0){
            return ResultBuildVO.error("情刷新后修改");
        }
        return ResultBuildVO.success();
    }

    @Override
    public ResultVO updateStatus(KuQuUpdatestatus kuQuUpdatestatus) {
        Date now = new Date();
        KuQu legalPlant1 = new KuQu();
        BeanUtils.copyProperties(kuQuUpdatestatus,legalPlant1);
        legalPlant1.setUpdateName("从session获取");
        legalPlant1.setUpdateTime(now);
        kuQuMapper.updateStatus(legalPlant1);
        return ResultBuildVO.success();
    }


}
